package com.acme.superchat.service.server;

import com.acme.superchat.service.exception.NickExistsException;

import java.util.List;

/**
 * @author Gregory Sobko
 */
public class ServerConnector {
    SharedServer sharedServer;
    ChatWorker worker;

    public ServerConnector(SharedServer sharedServer, ChatWorker worker) {
        this.sharedServer = sharedServer;
        this.worker = worker;
    }

    public void sendSelf(String message){
        worker.sendMessage(message);
    }

    public void broadcast(String command) {
        sharedServer.broadcast(this.worker, command);
    }
    public void printHistory() {
        worker.sendList(this.getHistory());
    }
    public List<String> getHistory() {
        return this.sharedServer.getHistory(worker);
    }

    public void setNick(String nick) {
        try{
            String prevname = worker.getName();
            this.sharedServer.setName(worker,nick);
            this.sharedServer.broadcast(worker, "User "+prevname+" changed nick to: " + nick);
        }catch(NickExistsException e){
            this.sendSelf("Nick '"+ nick + "' is already in use!");
        }

    }

    public void changeRoom(String roomName) {
        sharedServer.changeRoom(worker, roomName);
        worker.sendMessage("Room was changed to " + roomName);
    }
}
